package com.ipt.frm.pw;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class PrimerServlet
 */
public class RequestInfo extends ServletMaestro {

       
    /**
	 * 
	 */
	private static final long serialVersionUID = -7222970621860173660L;

	/**
     * @see HttpServlet#HttpServlet()
     */
    public RequestInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<head>");
        out.println("<title>Request Information Example</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h3>Request Information Example</h3>");
        out.println("Method: " + request.getMethod());
        out.println("</BR>");
        out.println("Request URI: " + request.getRequestURI());
        out.println("</BR>");
        out.println("Protocol: " + request.getProtocol());
        out.println("</BR>");
        out.println("PathInfo: " + request.getPathInfo());
        out.println("</BR>");
        out.println("Remote Address: " + request.getRemoteAddr());
        out.println("</BR>");
        out.println("</body>");
        out.println("</html>");
        
        
        
        //Recogida de parametros en get por url o por fomrulario
        String nombre="";
        String password="";
        String cabecera ="";
        if("enviarGET".equals(request.getParameter("submit"))){
        cabecera = "Recogida por formulario";
        nombre = request.getParameter("nombre");
        password = request.getParameter("pass");
        }
        else if(request.getParameter("param1")!=null & request.getParameter("param2")!=null){
        cabecera = "Recogida por URL";
        nombre = request.getParameter("param1");
        password = request.getParameter("param2");
        }
      		response.setContentType("text/html");
           // PrintWriter out2 = response.getWriter();
            out.println("<html>");
            out.println("<body>");
            out.println("<head>");
            out.println("<title>Informacion recogida por GET</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>" + cabecera + "</h1>");
            out.println("</BR>");
            out.println("Nombre: " + nombre);
            out.println("</BR>");
            out.println("Passwor: " + password);
            out.println("</BR>");

           
            
            Map params = request.getParameterMap();
            Iterator it= params.entrySet().iterator();
            while( it.hasNext()){
            	Map.Entry e=(Map.Entry) it.next();
            	out.print("["+ e.getKey() + "]" + e.getValue() + "</BR>");
            }
            
            Enumeration enume = request.getHeaderNames();
            while (enume.hasMoreElements()){
            	String name = (String) enume.nextElement().toString();
            	String value = request.getHeader(name);
            	out.println( name + " " +value + "</BR>");
            	
            }
            
            
            out.print("<h3> Variables de Session </h3>");
            
            HttpSession session = request.getSession(true);
         
            //guardar en session el atributo recogido por formulario
            session.setAttribute("user", nombre);
            
            //print de session info
            Date created = new Date(session.getCreationTime());
            Date accessed = new Date(session.getLastAccessedTime());
            out.println("ID " + session.getId());
            out.println("Created: " + created);
            out.println("Last Accessed: " + accessed);

            out.println("</BR>");
            out.println("</body>");
            out.println("</html>");  
	}
	
		

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//Puenteando las peticiones POST a doGeT
		//doGet(request, response);
		
		//Recogida de parametros normal por post
		String nombre = request.getParameter("nombre");
		String password = request.getParameter("pass");
		String opcionSelecionada = request.getParameter("select");
		
		response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<head>");
        out.println("<title>Informacion recogida por POST</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("Nombre: " + nombre);
        out.println("</BR>");
        out.println("Passwor: " + password);
        out.println("</BR>");
        out.println("opcion selecionada: " + opcionSelecionada);
        out.println("</BR>");
        out.println("</BR>");
        out.println("</body>");
        out.println("</html>");
		
		
	}

}
